import { savePermission } from '@/services/api/api-admin-permission';

import ProForm, {
  DrawerForm,
  ProFormDigit,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { Button, message } from 'antd';
import React from 'react';

interface AddPermissionProps {
  parent: AdminPermissionData.Permission;
  onRefurbish: () => void;
}
const AddPermission: React.FC<AddPermissionProps> = ({ parent, onRefurbish }) => {
  const selectAccesssType = [
    {
      label: '页面',
      value: 1,
    },
    {
      label: '模块',
      value: 2,
    },
    {
      label: '按钮',
      value: 3,
    },
    {
      label: '方法',
      value: 4,
    },
  ];

  return (
    <DrawerForm
      key="add_role"
      title="新建"
      initialValues={{
        parentName: parent.name,
        seq: 250,
      }}
      drawerProps={{
        destroyOnClose: true,
      }}
      trigger={<Button type="primary">新建</Button>}
      onFinish={async (values) => {
        const response = await savePermission({
          ...values,
          parentId: parent.id,
        });
        if (response.code === 0) {
          message.success('提交成功');
          onRefurbish();
        }
        return true;
      }}
    >
      <ProForm.Group>
        <ProFormText width="md" name="parentName" label="父级权限" disabled />
        <ProFormSelect
          width="md"
          options={selectAccesssType}
          name="type"
          label="类型"
          rules={[{ required: true, message: '请选择类型' }]}
        />
        <ProFormDigit width="md" label="排序" name="seq" min={1} max={250} />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText
          width="md"
          name="name"
          label="名称"
          rules={[{ required: true, message: '请输入名称' }]}
        />
        <ProFormText
          width="md"
          name="value"
          label="代码"
          rules={[{ required: true, message: '请输入代码' }]}
        />
        <ProFormTextArea width="xl" name="describes" label="描述" />
      </ProForm.Group>
    </DrawerForm>
  );
};
export default AddPermission;
